<?php
/**
 * 转账管理
 */
namespace app\common\model;

use think\Model;

class MemberTransaction extends Model
{
    protected $autoWriteTimestamp = 'datetime';
    protected $updateTime = false;

    /**
     * 添加用户交易记录
     * @param $mid  用户ID
     * @param $money    金额
     * @param $num 数量
     * @param $currency 币种
     * @param $transaction_state 1买 2卖
     * @param int $charge_money   手续费
     * @param int $state    1直接交易2委托交易
     * @param int $entrust_id   委托Id
     * @param int $sell_mid   卖方ID
     * @return int|string
     */
    public static function add_transaction($mid,$currency,$money,$num,$transaction_state,$charge_money = 0,$state,$entrust_id = 0,$sell_mid){
        $data = [
            'mid'   =>  $mid,
            'currency'  =>  $currency,
            'money' =>  $money,
            'num' =>  $num,
            'transaction_state' =>  $transaction_state,
            'charge_money'  =>  $charge_money,
            'state'  =>  $state,
            'entrust_id'  =>  $entrust_id,
            'sell_mid'  =>  $sell_mid
        ];
        return self::create($data);
    }

    /**
     *查询当天交易的数据
     */
    public static function get_transaction($mid,$transaction_state)
    {
        return self::where(['mid'=>$mid,'transaction_state'=>$transaction_state])->whereDay('create_time')->sum('num');
    }

    /**
     *查询当天交易总额
     */
    public static function get_transaction_num($transaction_state)
    {
        return self::where(['transaction_state'=>$transaction_state])->whereDay('create_time')->sum('num');
    }


    /**
     * 交易记录列表
     */
    public static function transaction_record_list($where,$page,$limit)
    {
        $data['data'] = self::where($where)->page($page,$limit)->order('create_time desc')->select()->toArray();

        $data['count'] = self::where($where)->count();

        return $data;
    }
}